package com.blensmile.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.blensmile.utils.LogUtils;
import com.blensmile.R;
import com.blensmile.utils.MediaPlayerHelper;

/**
 * Created by Blensmile on 2016/12/26.
 */

public class PlayVoiceView extends LinearLayout implements MediaPlayerHelper.CallBack{

    private View rlVoice;
    private TextView tvTime;
    private ImageView imgVoice;
    private ViewFlipper vfVoice;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0x11:
                    startPlayingAnim();
                    break;
                case 0x22:
                    setDefaultIcon();
                    break;
            }
        }
    };
    private String soundUrl = "";
    private String time = "";
    private MediaPlayerHelper helper;
    private static long lastClick = 0;
    private RotateAnimation rotateAnim;

    public PlayVoiceView(Context context) {
        this(context,null);
    }

    public PlayVoiceView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PlayVoiceView(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.voice_layout,this);
        imgVoice = (ImageView)findViewById(R.id.img_voice);
        vfVoice = (ViewFlipper)findViewById(R.id.vf_voice);
        tvTime = (TextView)findViewById(R.id.tv_time);
        rlVoice = findViewById(R.id.rl_voice);
        helper = new MediaPlayerHelper(PlayVoiceView.this);
        rotateAnim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnim.setDuration(1600);
        rotateAnim.setRepeatCount(-1);
        rotateAnim.setInterpolator(new LinearInterpolator());

        rlVoice.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(soundUrl)){
//                    if(System.currentTimeMillis()-lastClick<800){
//                        return;
//                    }
//                    lastClick = System.currentTimeMillis();
                    //判断是否是重复点击
                    if (helper.isRepetition()) {
                        helper.stopAndRelease();
                    } else {
                        startPreparingAnim();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                helper.play(getContext(), soundUrl);
                            }
                        }).start();

                    }
                }
            }
        });
    }

    /***开始播放动画***/
    private void startPlayingAnim(){
        imgVoice.clearAnimation();
        imgVoice.setVisibility(INVISIBLE);
        vfVoice.startFlipping();
        vfVoice.setVisibility(VISIBLE);
    }

    /***开始旋转的花儿***/
    private void startPreparingAnim(){
        imgVoice.setImageResource(R.drawable.aliwx_img_loading);
        imgVoice.startAnimation(rotateAnim);
        vfVoice.setVisibility(INVISIBLE);
    }

    private void setDefaultIcon(){
        vfVoice.stopFlipping();
        vfVoice.setVisibility(INVISIBLE);
        imgVoice.clearAnimation();
        imgVoice.setImageResource(R.drawable.voice3b);
        imgVoice.setVisibility(VISIBLE);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if(!TextUtils.isEmpty(MediaPlayerHelper.mPlayingUrl) && MediaPlayerHelper.mPlayingUrl.equals(getSoundUrl())){
            if(helper.isPlaying()){
                startPlayingAnim();
            }else {
                startPreparingAnim();
            }
            helper.registerCallBack();
        }else {
            setDefaultIcon();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        setDefaultIcon();
    }

    public String getTime() {
        return time;
    }

    public void setTime(String t) {
        this.time = t+"\"";
        tvTime.setText(time);
    }

    public void setTime(long t) {
        this.time = t + "\"";
        tvTime.setText(time);
    }

    public String getSoundUrl() {
        return soundUrl;
    }

    public void setSoundUrl(String soundUrl) {
        this.soundUrl = soundUrl;
    }

    /**实现MediaPlayerUtils的回调接口**/
    @Override
    public void onPlay() {
        handler.sendEmptyMessage(0x11);
    }

    @Override
    public void onStop() {
        handler.sendEmptyMessage(0x22);
    }

    @Override
    public void onErro() {
        handler.sendEmptyMessage(0x22);
    }

}
